import CryptoJS from 'crypto-js'

const key = CryptoJS.enc.Utf8.parse("1GUBHBQTQZO5OGPM");
const iv = CryptoJS.enc.Utf8.parse("F1NAOSIZHTALCDTP");
/**
 * AES加密处理
 */
export function encrypt(data: string) {
    const plaintext = JSON.stringify(data);
    const srcs = CryptoJS.enc.Utf8.parse(plaintext);
    const encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    const hexStr = encrypted.ciphertext.toString().toUpperCase();
    const oldHexStr = CryptoJS.enc.Hex.parse(hexStr);
    const base64Str = CryptoJS.enc.Base64.stringify(oldHexStr);
    return base64Str;
}
/**
 * AES解密处理
 */
export function decrypt(ciphertext: string) {
    const srcs = ciphertext;
    const decrypt = CryptoJS.AES.decrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
    const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    const value = decryptedStr.toString();
    return JSON.parse(value);
}